<?php
class ControllerLocalisationWeightClass extends AdminController {

	public function index() {

		M('localisation/weight_class');

		$this->getList();
	}

	public function insert() {

		M('localisation/weight_class');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_weight_class->addWeightClass($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/weight_class'));
		}

		$this->getForm();
	}

	public function update() {

		M('localisation/weight_class');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_weight_class->editWeightClass($this->request->get['weight_class_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/weight_class'));
		}

		$this->getForm();
	}

	public function delete() {

		M('localisation/weight_class');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $weight_class_id) {
				$this->model_localisation_weight_class->deleteWeightClass($weight_class_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/weight_class'));
		}

		$this->getList();
	}

	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$weight_class_total = $this->model_localisation_weight_class->getTotalWeightClasses();
		$this->data['weight_classes'] = $this->model_localisation_weight_class->getWeightClasses($qf);

		$this->data['pagination'] = $this->pagination($weight_class_total)->render();

		$this->display('localisation/weight_class_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['weight_class_id'])) {
			$this->data['action'] = UA('localisation/weight_class/update', 'weight_class_id=' . $this->request->get['weight_class_id']);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_localisation_weight_class->getWeightClass($this->request->get['weight_class_id']);
				$this->vo->data['weight_class_description'] = $this->model_localisation_weight_class->getWeightClassDescriptions($this->request->get['weight_class_id']);
			}
		} else {
			$this->data['action'] = UA('localisation/weight_class/insert');
		}

		$this->data['languages'] = C('cache_language_2');

		$this->display('localisation/weight_class_form.tpl');
	}

	protected function validateForm() {

		$pass = true;

		foreach ($this->request->post['weight_class_description'] as $language_id => $value) {
			if (!range_length($value['title'],  3, 32)) {
				$this->setMessage('error_title_'.$language_id, $this->_['error_title']);
				$pass = false;
			}

			if (!$value['unit'] || (utf8_strlen($value['unit']) > 4)) {
				$this->setMessage('error_unit_'.$language_id, $this->_['error_unit']);
				$pass = false;
			}
		}

		return $pass;
	}

	protected function validateDelete() {

		M('catalog/product');

		foreach ($this->request->post['selected'] as $weight_class_id) {
			if (C('config_weight_class_id') == $weight_class_id) {
				$this->setMessage('error_warning', $this->_['error_default']);
				return false;
			}

			$product_total = $this->model_catalog_product->getTotalProductsByWeightClassId($weight_class_id);

			if ($product_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_product'], $product_total));
				return false;
			}
		}

		return true;
	}
}
?>